<?php


namespace app\api\service;


use app\common\service\Redis;
use think\facade\Cache;


class Lua
{
    private const LUCKY_RED_ENVELOPE_JOB_SCRIPT    = null;
    private const ORDINARY_RED_ENVELOPE_JOB_SCRIPT = null;
    private const LUCKY_RED_ENVELOPE_JOB           = <<<'rob'
local red_envelopes_id = tostring(KEYS[1]);
local user_id = tostring(KEYS[2]);

    -- 检查是否有足够的库存
local number = tonumber(redis.call('hGet','red_envelope_lucky_'..red_envelopes_id,'number'));
if number == nil or number == false then
    return false;
end
local rob_number = tonumber(redis.call('lLen','red_envelope_info_lucky_'..red_envelopes_id));
if number <= rob_number then
    -- 无库存
    return 0;
elseif number - rob_number == 1 then
    -- 抢到最后一个红包
    return 2;
else
    -- 抢到红包
    return 1;
end
rob;
    private const ORDINARY_RED_ENVELOPE_JOB        = <<<'rob'
    red_envelopes_info = cjson.decode(red_envelopes_info);
return red_envelopes_info;
rob;

    /**
     * 0-没有库存，失败
     * 1-成功
     * 2-成功，并且是最后一个红包
     * @param $red_envelopes_id
     * @param $user_id
     * @return bool
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/4/28
     * @Time: 2:49
     */
    public static function robLuckyRedEnvelope($red_envelopes_id, $user_id): bool
    {
        /**
         * (hash)red_envelope_{:type}_{:红包id} 红包信息
         *      user_id:
         *      total_amount:
         *      number:
         *
         * (list)red_envelope_info_{:type}_{红包id} 抢红包的信息
         *      user_id:
         *      type:
         *      rob_amount:
         *      rob_timestamp:
         */
        //$redis = Cache::store('redis')->handler();
        $redis = Redis::getRedis();
        //var_dump($redis->hGetAll('red_envelope_lucky_49'));die;
        $result = $redis->eval(self::LUCKY_RED_ENVELOPE_JOB, [
            49,
            $user_id
        ], 2);
        var_dump($result,$redis->getLastError());die;
        switch ($result) {
            case 0:
                #无库存
                break;
            case 1:
                #抢到红包
                break;
            case 2:
                #抢到最后一个红包
            break;
            default:
                #红包不存在
                break;
        }
        return $result ? true : false;
    }

    /**
     * (hash)ordinary_red_envelope_{红包id} 红包信息
     *      user_id:
     *      single_amount:
     *      number:
     *
     * (list)red_envelope_info_{红包id} 抢红包的信息
     *      user_id:
     *      type:
     *      rob_amount:
     *      rob_timestamp:
     * @return bool
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/4/28
     * @Time: 11:57
     */
    public function robOrdinaryRedEnvelope(): bool
    {
        $result = Cache::store('redis')->handler()->eval(self::ORDINARY_RED_ENVELOPE_JOB, [
            1,
            2
        ], 2);
        switch ($result) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                break;
            default:
                break;
        }
        return $result ? true : false;
    }
}